<?

//声明启用session
session_start();

//鉴权
include_once '../controller/script/check_session.php';


//载入根目录config设定
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");

//**获取当前板块资料，必然载入，后续很多读取板块数据库的表格，都写成动态的了。
$_plate=$plate['asset'];




//根据筛选框传递过来的内容，组装查找语句
include_once("script/set_condition.php");


//根据页面信息，组装查找语句
include_once("script/set_page.php");


//根据页面数量信息，筛选信息，创造url，供页码使用
include_once("script/set_url.php");
include_once("script/set_order.php");



$asset_setting=select_one_line_all_items($table_prefix."asset_setting","nid > 0");



$setting=select_one_line_all_items($table_prefix."setting","nid=1");

//按结品牌树
$brand_list=select_lines_all_items($table_prefix."brand","nid desc","parent_id = 0 and status = '启用'","");
$brand_name_list=transform_2darray_in_1darray_with_new_key($brand_list,"nid","name");

//$brand_list=transform_2darray_in_1darray_number_as_key($brand_list,"name");

//循环，查找数据库中，parent_nid = 当前的 nid的值
foreach($brand_list as $key=>$value){
    $brand_list[$key][$value['name']]=select_lines_all_items($table_prefix."brand","listorder desc,name asc,nid desc",
                                                             "parent_id = '{$value['nid']}'","");

    //循环，$brand_list[$key]['grade'] 的数据，查找数据库中，parent_nid = 当前的 nid的值，记录在class
    foreach($brand_list[$key][$value['name']] as $k=>$v){
        $brand_list[$key][$value['name']][$k][$v['name']]=select_lines_all_items($table_prefix."brand",
                                                                                 "listorder desc,name asc,nid desc","parent_id = '{$v['nid']}'","");
    }
}

//把设置种的标签拆成数组，用于循环比较
$tag_list = explode(";", $asset_setting['tag']);
$file_type_list = explode(";", $asset_setting['file_type']);
$asset_type_list = explode(";", $asset_setting['asset_type']);
$asset_class_list = explode(";", $asset_setting['asset_class']);




if($_REQUEST['brand'])
{
    $_brand_condition=" and brand like '%{$_REQUEST['brand']}%'";
};


if($_REQUEST['duration'] && $_REQUEST['duration'] >0)
{
    $_duration_condition=" and duration = $_REQUEST[duration]";
};

if($_REQUEST['tag'])
{
    $_tag_condition=" and tag = $_REQUEST[tag]";
};

$_date_condition = '';
if($_REQUEST['date_select'] == 'today')
{
    $_date_condition=" and date like '".date("Y-m-d")."%%'";
}
elseif($_REQUEST['date_select'] == 'this_week')
{
    $_date_condition=" and YEARWEEK(date,1) = YEARWEEK(CURDATE(),1)";
}

if(isset($_REQUEST['date_start']) && $_REQUEST['date_start'] != '' )
{
    $_date_condition.=" and date >= $_REQUEST[date_start]";
}
if(isset($_REQUEST['date_end']) && $_REQUEST['date_end'] != '' )
{
    $_date_condition.=" and date <= $_REQUEST[date_end]";
}


if($_REQUEST['asset_class'])
{
    $type_condition = "and (";
    foreach($_REQUEST['asset_class'] as $type_key=>$type){
        $type_condition .= " asset_class like '%%".$type."%%'";
        if(count($_REQUEST['asset_class']) >1 && ($type_key < (count($_REQUEST['asset_class'])-1))){
            $type_condition .= " or ";
        }
    }

    $type_condition .= ")";

};

$_condition = $_brand_condition.$_duration_condition.$_date_condition.$type_condition.$_tag_condition;

$_asset_list=select_lines_all_items($table_prefix."asset_data","listorder desc,title asc,nid desc",
    $_condition,"$_data_start,$_quantity_per_page");




//产品分类
//直接获取产品分类的树，并且如果其最后一级没有子分类的话，则出产品。
$category_list=select_lines_all_items($table_prefix."term",
    "nid desc","interm = 'root' and publish = '1' ","");


//循环，查找数据库中，parent_nid = 当前的 nid的值
foreach($category_list as $key=>$value)
{
    $category_list[$key]['level2'] = select_lines_all_items($table_prefix . "term", "", "term1 = {$value['nid']} && interm = 'term1'");
    //如果当前分类没有子分类，则出产品。
    if(count($category_list[$key]['level2']) == 0)
        {
            //echo $value['name']."没有子分类";

            //读取产品
            $_relation=select_lines_all_items($table_prefix."relation",
                "nid desc","term_nid = {$value['nid']}","");
            $_relation_product_nid_list=transform_2darray_in_1darray_number_as_key($_relation,"detail_nid");
            $_product_list = select_lines_all_items($table_prefix . "detail", "nid desc",
                                                    "nid in ('" . implode("','", $_relation_product_nid_list) . "')","");
            $category_list[$key]['level2']=$_product_list;
            continue;
        }

    foreach($category_list[$key]['level2'] as $k=>$v)
    {
        $category_list[$key]['level2'][$k]['level3'] = select_lines_all_items($table_prefix . "term", "", "term2 = {$v['nid']} && interm = 'term2'");

        //如果当前分类没有子分类，则出产品。
        if(count($category_list[$key]['level2'][$k]['level3']) == 0)
        {
            //echo $v['name']."没有子分类";
            //读取产品
            $_relation=select_lines_all_items($table_prefix."relation",
                                              "nid desc","term_nid = {$v['nid']}","");
            $_relation_product_nid_list=transform_2darray_in_1darray_number_as_key($_relation,"detail_nid");
            $_product_list = select_lines_all_items($table_prefix . "detail", "nid desc",
                                                    "nid in ('" . implode("','", $_relation_product_nid_list) . "')","");
            $category_list[$key]['level2'][$k]['level3']=$_product_list;
            continue;

        }
        foreach($category_list[$key]['level2'][$k]['level3'] as $kk=>$vv)
        {
            $category_list[$key]['level2'][$k]['level3'][$kk]['level4'] = select_lines_all_items($table_prefix . "term", "", "term3 = {$vv['nid']} && interm = 'term3'");

            //如果当前分类没有子分类，则出产品。
            if(count($category_list[$key]['level2'][$k]['level3'][$kk]['level4']) == 0)
            {
                //echo $vv['name']."没有子分类";
                //读取产品
                $_relation=select_lines_all_items($table_prefix."relation",
                                                  "nid desc","term_nid = {$vv['nid']}","");
                $_relation_product_nid_list=transform_2darray_in_1darray_number_as_key($_relation,"detail_nid");
                $_product_list = select_lines_all_items($table_prefix . "detail", "nid desc",
                                                    "nid in ('" . implode("','", $_relation_product_nid_list) . "')","");
                $category_list[$key]['level2'][$k]['level3'][$kk]['level4']=$_product_list;
                continue;
            }
            foreach($category_list[$key]['level2'][$k]['level3'][$kk]['level4'] as $kkk=>$vvv)
            {
                $category_list[$key]['level2'][$k]['level3'][$kk]['level4'][$kkk]['level5'] = select_lines_all_items($table_prefix . "term", "", "term4 = {$vvv['nid']} && interm = 'term4'");
                //如果当前分类没有子分类，则出产品。
                if(count($category_list[$key]['level2'][$k]['level3'][$kk]['level4'][$kkk]['level5']) == 0)
                {
                    //echo $vvv['name']."没有子分类";
                    //读取产品
                    //读取产品
                    $_relation=select_lines_all_items($table_prefix."relation",
                                                      "nid desc","term_nid = {$vvv['nid']}","");
                    $_relation_product_nid_list=transform_2darray_in_1darray_number_as_key($_relation,"detail_nid");

                    $_product_list = select_lines_all_items($table_prefix . "detail", "nid desc",
                                                            "nid in ('" . implode("','", $_relation_product_nid_list) . "')","");
                    $category_list[$key]['level2'][$k]['level3'][$kk]['level4'][$kkk]['level5']=$_product_list;
                    continue;


                }

            }
        }
    }

}


$category_name_list=transform_2darray_in_1darray_with_new_key($category_list,'nid','name');

$product_list=select_lines_all_items($table_prefix."detail","nid desc","","");
$product_name_list=transform_2darray_in_1darray_with_new_key($product_list,'nid','name');

 
?>



